package thread.synchronize;

/**
 * @author liweimin
 * @date 2021/4/27 11:54
 * @since
 */
public class SynchronizeTest {
    public static final Object lock = new Object();

    public static void main(String[] args) {
        SynchronizeTest synchronizeTest = new SynchronizeTest();
        SynchronizeTest synchronizeTest1 = new SynchronizeTest();
        new Thread(synchronizeTest::mm, "A").start();
        new Thread(synchronizeTest1::mm, "B").start();
        new Thread(SynchronizeTest::m, "C").start();
        new Thread(synchronizeTest::mm1).start();
        new Thread(synchronizeTest::mm2).start();
    }

    public void mm() {
        synchronized (lock) {
            for (int i = 0; i < 100; i++) {
                System.out.println(Thread.currentThread().getName() + "==>" + i);
            }
        }
    }

    public synchronized void mm1() {
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + "==>" + i);
        }
    }

    public void mm2() {
        synchronized (this) {
            for (int i = 0; i < 100; i++) {
                System.out.println(Thread.currentThread().getName() + "==>" + i);
            }
        }
    }

    public static void m() {
        synchronized (lock) {
            for (int i = 0; i < 100; i++) {
                System.out.println(Thread.currentThread().getName() + "==>" + i);
            }
        }
    }
}
